package weather.gui.panels;
import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JTextPane;


@SuppressWarnings("serial")
public class QuestionPanel extends javax.swing.JPanel {
	private JTextPane textPanel;
	private ChatPanel chatPanel;

	public void setChatPanel(ChatPanel chatPanel) {
		this.chatPanel = chatPanel;
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new QuestionPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public QuestionPanel() {
		super();
		initGUI();
		textPanel.addKeyListener(new EnterPressedHandler());
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(400, 300));
			{
				textPanel = new JTextPane();
				this.add(new JScrollPane(textPanel), BorderLayout.CENTER);
				textPanel.setBackground(new Color(0xF5DEB3));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private class EnterPressedHandler extends KeyAdapter
	{
		public void keyPressed(KeyEvent event)
		{
			int keyCode = event.getKeyCode();
			if(keyCode == KeyEvent.VK_ENTER)
			{
				if(event.isShiftDown())
				{
					textPanel.setText(textPanel.getText()+"\n");
					event.consume();
					return;
				}
				chatPanel.onQuestion(textPanel.getText());
				textPanel.setText("");
				event.consume();
			}
		}
	}

}
